/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __QS_PortfolioWidget_H__
#define __QS_PortfolioWidget_H__

#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtGui/QLineEdit>
#include <QtGui/QTableWidget>
#include <QtGui/QTableWidgetItem>

#include "PortfolioManager.h"
#include "QueryManager.h"
#include "DiagramDialog.h"
#include "StockShareDialog.h"
#include "PriceIntervalDialog.h"
#include "StockDayTableDialog.h"

namespace QSThink
{
	
	class PortfolioWidget : 
		public QWidget,
		public PortfolioListener
	{
		Q_OBJECT

	public:
		PortfolioWidget(QWidget *parent=0,Qt::WindowFlags flag=0);
		virtual ~PortfolioWidget();

		void setupDataTable();
	
	protected:
		void loadPortfolio();
		void setupInputCompleter();
		
		void insertTableRow( QString symbol );
		
	protected:
		void OnPortfolioAdded(QString symbol);
		void OnPortfolioRemoved(QString symbol);
		void OnPortfolioUpdated(PortfolioItem* item);

	protected:
		typedef std::map<QString,QTableWidgetItem*> ItemMap;
		ItemMap _itemMap;//find account item(row) by symbol string
		QLineEdit* _lineEditSymbol;
		QComboBox* _comboSymbol;
		QTableWidget* _dataTable;
		QLabel* _labelCapital;//show all your capital
		QCompleter* _completer;

		//need a dialog manager?
		DiagramDialog* _diagramViewDialog;
		StockShareDialog* _equityViewDialog;
		PriceIntervalDialog* _priceIntervalDialog;
		StockDayTableDialog* _stockDayTableDialog;

		

		QString _symbol;//current symbol;
		bool _symbolSelected;//start edit;

		QString _selectedText;
		bool _itemEdit;

		unsigned int _lastTime;
		float _frameRate;
		bool _inited;
	protected slots:
		void addSymbol();
		void removeSymbol();
		void viewStockPriceGraph();
		void viewStockPriceExGraph();
		void viewStockPriceExInterval();
		
		void viewStockCapitalGraph();
		void viewStockShareChange();
		void viewStockDayPriceTable();
		void symbolSelected(const QModelIndex &);
		void symbolChanged( const QString & symbol );
		void symbolEdited( const QString &symbol );
		void tableItemChanged( QTableWidgetItem * item );
		void tableItemDoubleClicked( QTableWidgetItem * item );
		void capitalCalc();
	};
}
#endif
